package com.company;

import java.util.ArrayList;
import java.util.List;


public class Items {

    public List<Item> items = new ArrayList<Item>();

    BooksLog log = BooksLog.getInstance();

    Items() {

    }

    public int listAllItems() {
        int i = 1;
        int count = 0;
        for (Item item : items) {
            System.out.println(" ");
            item.print(i++);
            count = count + 1;

        }
        System.out.println(" ");
        return count;
    }


    public int listAvilableItems() {
        int i = 1;
        int count = 0;
        for (Item item : items) {
            if (item.matchesForAvailable()) {
                System.out.println(" ");

                item.print(i++);

                count = count + 1;
            }
        }
        System.out.println(" ");
        return count;
    }


    public boolean checkoutItem(String libraryNo, String name) {
        for (Item item : items) {
            if (item.matchesForCheckout(name)) {
                item.chechoutItem();
                log.addEntry(name, libraryNo);
                return true;
            }
        }
        return false;
    }

    public boolean returnItem(String libraryNo, String name) {
        try {
            if (log.bookslog.get(name).equals(libraryNo)) {
                for (Item item : items) {
                    if (item.matchesForReturn(name)) {
                        item.returnItem();
                        log.removeEntry(name);
                        return true;
                    }

                }
            }
        } catch (Exception e) {
            System.out.println("Book is not present");
        }
        return false;
    }

    public void add(Item item)
    {
        items.add(item);
    }
}
